<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增媒体服务器
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-curent-row
    >
      <el-table-column align="center" label="序号" min-width="5%">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务器id" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.server_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主机地址" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.media_host }}</span>
        </template>
      </el-table-column>
      <el-table-column label="http 端口" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.http_port }}</span>
        </template>
      </el-table-column>
      <el-table-column label="https 端口" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.https_port }}</span>
        </template>
      </el-table-column>
      <el-table-column label="rtmp 端口" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.rtmp_port }}</span>
        </template>
      </el-table-column>
      <el-table-column label="rtp 端口" align="center" min-width="19%">
        <template slot-scope="scope">
          <span>{{ scope.row.rtp_min_port }} - {{ scope.row.rtp_max_port }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="210" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="130px" style="margin-left:50px;margin-right:50px">
        <el-form-item label="主机地址" prop="media_host">
          <el-input v-model="temp.media_host" />
        </el-form-item>
        <el-form-item label="秘钥" prop="secret">
          <el-input v-model="temp.secret" />
        </el-form-item>
        <el-form-item label="http服务端口号" prop="http_port">
          <el-input v-model="temp.http_port" />
        </el-form-item>
        <el-form-item label="服务器配置" prop="config">
          <el-button @click="loadServerConfig()">点击查询服务器配置</el-button>
        </el-form-item>
        <el-form-item label="服务器Id" prop="server_id">
          <el-input v-model="temp.server_id" />
        </el-form-item>
        <el-form-item label="https服务端口号" prop="https_port">
          <el-input v-model="temp.https_port" />
        </el-form-item>
        <el-form-item label="rtmp服务端口号" prop="rtmp_port">
          <el-input v-model="temp.rtmp_port" />
        </el-form-item>
        <el-form-item label="rtp起始端口" prop="rtp_min_port">
          <el-input v-model="temp.rtp_min_port" />
        </el-form-item>
        <el-form-item label="rtp截止端口" prop="rtp_max_port">
          <el-input v-model="temp.rtp_max_port" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, del, insert, update } from '@/api/mediaserver'
import { getServerInfo } from '@/api/media'
import { Message } from 'element-ui'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      pagesize: 10,
      page: 1,
      listLoading: true,

      temp: {
        server_id: undefined,
        media_host: undefined,
        secret: undefined,
        http_port: 9094,
        https_port: 9096,
        rtmp_port: 9092,
        rtp_min_port: 50000,
        rtp_max_port: 50500
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改媒体服务器',
        create: '新增媒体服务器'
      },
      rules: {
        server_id: [{ required: true, message: 'ServerId不能为空', trigger: 'change' }],
        media_host: [{ required: true, message: '主机地址不能为空', trigger: 'change' }],
        http_port: [{ required: true, message: 'http 服务端口号不能为空', trigger: 'change' }],
        rtmp_port: [{ required: true, message: 'rtmp 服务端口号不能为空', trigger: 'change' }],
        rtp_min_port: [{ required: true, message: 'rtp 起始端口号不能为空', trigger: 'change' }],
        rtp_max_port: [{ required: true, message: 'rtp 截止端口号不能为空', trigger: 'change' }]
      }
    }
  },

  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const data = { page: this.page, pagesize: this.pagesize }
      list(data).then(response => {
        this.list = response.data.items
        this.list.forEach((item) => {
          item.edit = false
        })
        this.listLoading = false
        this.total = response.data.total
      })
    },
    current_change: function(page) {
      this.page = page
      this.fetchData()
    },
    resetTemp() {
      this.temp = {
        server_id: undefined,
        media_host: undefined,
        secret: undefined,
        http_port: 9094,
        https_port: 9096,
        rtmp_port: 9092,
        rtp_min_port: 50000,
        rtp_max_port: 50500
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    loadServerConfig() {
      if (!this.temp.media_host) {
        Message({
          message: '请写填写主机地址',
          type: 'error',
          duration: 5 * 1000
        })
      } else {
        getServerInfo({
          'host': this.temp.media_host,
          'port': this.temp.http_port,
          'secret': this.temp.secret
        }).then((ret) => {
          if (ret.code === 20000) {
            const config = ret.data[0]
            this.temp.https_port = config['http.sslport']
            this.temp.rtmp_port = config['rtmp.port']
            this.temp.server_id = config['general.mediaServerId']
          }
        })
      }
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.listLoading = true
          insert(this.temp).then((ret) => {
            if (ret.code === 20000) {
              this.dialogFormVisible = false
              this.$notify({
                title: 'Success',
                message: 'Created Successfully',
                type: 'success',
                duration: 2000
              })
              this.listLoading = false
              this.fetchData()
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          this.listLoading = true
          update(this.temp).then((ret) => {
            if (ret.code === 20000) {
              this.dialogFormVisible = false
              this.$notify({
                title: 'Success',
                message: '更新操作成功',
                type: 'success',
                duration: 2000
              })
              this.listLoading = false
              this.fetchData()
            }
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('确认删除此媒体服务器信息？').then(_ => {
        this.listLoading = true
        del({ server_id: row.server_id }).then(response => {
          this.listLoading = false
          this.fetchData()
        })
      }).catch(_ => {

      })
    }
  }
}
</script>

